package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.annotation.SecureValid;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Schedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 孔德成
 * @slogan 致敬大师，致敬未来的你
 * @date 2021/9/3 9:53 上午
 * @desc
 */
@Api(tags = "医院数据上传")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    private final HospitalService hospitalService;
    private final DepartmentService departmentService;
    private final ScheduleService scheduleService;

    public ApiController(HospitalService hospitalService, DepartmentService departmentService, ScheduleService scheduleService) {
        this.hospitalService = hospitalService;
        this.departmentService = departmentService;
        this.scheduleService = scheduleService;
    }

    @SecureValid
    @ApiOperation("医院信息上传")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        // 将 parameterMap 转为 Map<String, Object>
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        // 图片数据处理
        // 传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = request.getParameter("logoData");
        logoData = logoData.replaceAll(" ", "+");
        map.put("logoData", logoData);
        // 上传医院信息
        hospitalService.save(map);
        return Result.ok();

    }

    @SecureValid
    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        return Result.ok(hospitalService.getByHoscode(request.getParameter("hoscode")));
    }

    @SecureValid
    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        departmentService.save(map);
        return Result.ok();
    }

    @SecureValid
    @ApiOperation(value = "分页查询科室列表")
    @PostMapping("department/list")
    public Result findDepartmentByPage(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        Page<Department> pageModel = departmentService.selectPage(map);
        return Result.ok(pageModel);
    }

    @SecureValid
    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        departmentService.remove(request.getParameter("hoscode"), request.getParameter("depcode"));
        return Result.ok();
    }

    @SecureValid
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        scheduleService.save(map);
        return Result.ok();
    }

    @SecureValid
    @ApiOperation(value = "分页查询排班")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        Page<Schedule> pageModel = scheduleService.selectPage(map);
        return Result.ok(pageModel);
    }

    @SecureValid
    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        scheduleService.remove(request.getParameter("hoscode"), request.getParameter("hosScheduleId"));
        return Result.ok();
    }
}
